package Testing;

import static org.junit.Assert.*;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

import model.ClassicMap;
import model.Continent;
import model.Map;
import model.Player;
import model.Territory;
import model.AI.Cyborg;
import model.AI.ExpertCyborg;
import model.AI.MapInfo;
import model.AI.SimpleCyborg;

import org.junit.Test;

public class ModelTest {

	// @Test
	public void GameCreationTest() {

		List<model.Player> playerList = new ArrayList<model.Player>();

		model.Player player1 = new model.HumanPlayer("David", Color.CYAN,
				new ImageIcon("images/Wall-E.png"));
		playerList.add(player1);

		model.Player player2 = new model.HumanPlayer("Chang", Color.GREEN,
				new ImageIcon("images/Wall-E.png"));
		playerList.add(player2);

		model.Player player3 = new model.HumanPlayer("Kevin", Color.PINK,
				new ImageIcon("images/Wall-E.png"));
		playerList.add(player3);

		model.Player player4 = new model.AI.SimpleCyborg("Cam", Color.GRAY,
				new ImageIcon("images/HAL.png"));
		playerList.add(player4);

		model.Player player5 = new model.AI.SimpleCyborg("AT-AT", Color.BLACK,
				new ImageIcon("images/HAL.png"));
		playerList.add(player5);
		model.Player player6 = new model.AI.SimpleCyborg("AT-ST", Color.BLACK,
				new ImageIcon("images/HAL.png"));
		playerList.add(player6);

		Map map = new ClassicMap(playerList);

		int territoryCounter = 0;
		for (model.Player tPlayer : playerList) {
			assertEquals(0, tPlayer.getNumCards());
			assertEquals(25, tPlayer.getBonus());
			assertEquals(25, tPlayer.getNumArmies());

			// System.out.println("\n" + tPlayer.getName() + "'s troops: " +
			// tPlayer.getNumArmies());
			// System.out.println("\n" + tPlayer.getName() +
			// "'s territories: ");
			for (model.Continent cont : map.listContinents()) {
				for (model.Territory terr : cont.listTerritory()) {
					if (terr.getRuler().equals(tPlayer)) {
						territoryCounter++;
						// System.out.println(terr.getTerritoryName());
					}
				}
			}
			// System.out.println("\n");
		}
		assertEquals(42, territoryCounter);
		assertEquals(44, map.getMapCards().size());
		map.giveCard(player1);
		map.giveCard(player2);
		assertEquals(42, map.getMapCards().size());
		assertEquals(1, player1.getNumCards());
		assertEquals(1, player2.getNumCards());

	}

	// @Test
	public void GameCombatMovementTest() {
		List<model.Player> playerList = new ArrayList<model.Player>();
		model.Player player1 = new model.HumanPlayer("David", Color.CYAN,
				new ImageIcon("images/Wall-E.png"));
		playerList.add(player1);

		model.Player player2 = new model.HumanPlayer("Chang", Color.GREEN,
				new ImageIcon("images/Wall-E.png"));
		playerList.add(player2);

		model.Player player3 = new model.HumanPlayer("Kevin", Color.PINK,
				new ImageIcon("images/Wall-E.png"));
		playerList.add(player3);

		Cyborg player4 = new SimpleCyborg("Cam", Color.GRAY, new ImageIcon(
				"images/HAL.png"));
		playerList.add((Player) player4);

		Cyborg player5 = new SimpleCyborg("AT-AT", Color.BLACK, new ImageIcon(
				"images/HAL.png"));
		playerList.add((Player) player5);

		Cyborg player6 = new ExpertCyborg("R2-D2", Color.RED, new ImageIcon(
				"images/HAL.png"));
		playerList.add((Player) player6);

		Map map = new ClassicMap(playerList);
		for (model.Player tPlayer : playerList) {

			System.out.println("\n" + tPlayer.getName() + "'s troops: "
					+ tPlayer.getNumArmies());
			System.out.println("\n" + tPlayer.getName() + "'s territories: ");
			for (model.Continent cont : map.listContinents()) {
				for (model.Territory terr : cont.listTerritory()) {
					if (terr.getRuler().equals(tPlayer)) {
						System.out.println(terr.getTerritoryName());
					}
				}
			}
			System.out.println("\n");
		}

		assertTrue(map.getContinent("Asia") != null);
		assertTrue(map.getContinent("North America") != null);
		assertTrue(map.getContinent("North America").getTerritoryInContinent(
				"Alaska") != null);
		model.Territory attacker = map.getContinent("North America")
				.getTerritoryInContinent("Alaska");
		assertTrue(attacker != null);
		model.Territory defender = map.getContinent("North America")
				.getTerritoryInContinent("Northwest Territory");
		assertEquals(0, attacker.getTroopNumber());
		attacker.addTroop(5);
		defender.addTroop(5);

		System.out.println("\n" + attacker.getRuler().getName() + "'s Troops: "
				+ attacker.getTroopNumber() + " "
				+ defender.getRuler().getName() + "'s Troops: "
				+ defender.getTroopNumber());
		new model.Combat(attacker, defender);
		System.out.println("\n" + attacker.getRuler().getName() + "'s Troops: "
				+ attacker.getTroopNumber() + " "
				+ defender.getRuler().getName() + "'s Troops: "
				+ defender.getTroopNumber());
		new model.Combat(attacker, defender);
		System.out.println("\n" + attacker.getRuler().getName() + "'s Troops: "
				+ attacker.getTroopNumber() + " "
				+ defender.getRuler().getName() + "'s Troops: "
				+ defender.getTroopNumber());

		model.Territory origin = map.getContinent("Oceania")
				.getTerritoryInContinent("Indonesia");
		model.Territory destination = map.getContinent("Asia")
				.getTerritoryInContinent("Siam");
		origin.addTroop(5);
		destination.addTroop(1);

		new model.Movement(origin, destination, 4);
		assertEquals(1, origin.getTroopNumber());
		assertEquals(5, destination.getTroopNumber());

		new model.Movement(origin, destination, 1);
		for (Continent temp : map.listContinents()) {
			for (Territory tempT : temp.listTerritory()) {
				tempT.addTroop(5);
			}
		}

		int counter = 0;
		while (counter < 25) {
			Thread thread4 = new Thread(player4);
			Thread thread5 = new Thread(player5);
			Thread thread6 = new Thread(player6);
			System.out.println("Bot Warfare Turn: " + (counter + 1));
			((Player) player4).addBonus(3);
			((Player) player5).addBonus(3);
			((Player) player6).addBonus(3);

			thread4.run();
			thread5.run();
			thread6.run();
			counter++;
		}

	}

	@Test
	public void botTest() throws InterruptedException {
		List<model.Player> playerList = new ArrayList<model.Player>();
		Cyborg player4 = new SimpleCyborg("HAL", Color.GRAY, new ImageIcon(
				"images/HAL.png"));
		playerList.add((Player) player4);

		Cyborg player5 = new SimpleCyborg("AT-AT", Color.BLACK, new ImageIcon(
				"images/HAL.png"));
		playerList.add((Player) player5);

		Cyborg player6 = new ExpertCyborg("R2-D2", Color.RED, new ImageIcon(
				"images/HAL.png"));
		playerList.add((Player) player6);

		Map map = new ClassicMap(playerList);

		int counter = 0;
		while (counter < 75) {
			if (counter > 13) {
				System.out.println("attacking");
			}
			if (counter == 34) {
				System.out.println();
			}
			System.out.println(map.getPhase());
			Thread thread4 = new Thread(player4);
			Thread thread5 = new Thread(player5);
			Thread thread6 = new Thread(player6);
			System.out.println("Bot Warfare Turn: " + (counter + 1)
					+ "\nUnowned Territories: "
					+ MapInfo.listUnownedTerritories((Player) player4).size());

			thread4.run();

			thread5.run();

			thread6.run();

			counter++;
		}

	}
}
